from copy import deepcopy
import aiohttp
import asyncio
from config import ZHUQUE_COOKIE, ZHUQUE_CSRF
import logging

logger = logging.getLogger("main")

cookie_headers = {
    "Cookie": ZHUQUE_COOKIE,
    "X-Csrf-Token": ZHUQUE_CSRF,
}


async def post(session: aiohttp.ClientSession, url, data=None, referer=None):
    headers = deepcopy(cookie_headers)
    headers["referer"] = referer
    async with session.post(url, headers=headers, json=data) as response:
        if response.status == 200:
            ret = await response.json()
            logger.info(ret)
            return ret
        else:
            logger.error(f"Error: {response.status} {await response.text()}")
            return None


async def create(type: int, unit="GiB", bonus=1000, upload=10, n=1):
    referer = "https://zhuque.in/bonus/transaction/bonus"
    url = "https://zhuque.in/api/transaction/create"
    data = {"type": type, "unit": unit, "bonus": bonus, "upload": upload}
    m = 0
    async with aiohttp.ClientSession() as session:
        for _ in range(n):
            if await post(session, url, data, referer):
                m += 1
        return m


if __name__ == "__main__":
    asyncio.run(create(1, bonus=765, upload=600000, n=1))
